package com.lovo.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.system.dto.PurchaseDto;
import com.lovo.system.mapper.PurchaseMapper;
import com.lovo.system.service.IPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, PurchaseDto> implements IPurchaseService {

    @Autowired
    PurchaseMapper purchaseMapper;

    @Override
    public List<PurchaseDto> info(int userId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_id",userId);
        return purchaseMapper.selectList(qw);
    }

    @Override
    public int selectRepeat(PurchaseDto purchaseDto) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("code",purchaseDto.getCode());
        qw.eq("user_id",purchaseDto.getUserId());
        int count = purchaseMapper.selectCount(qw);
        if (count == 1){
            purchaseMapper.addPurchaseNumber(purchaseDto.getCode(),purchaseDto.getUserId());
            return 0;
        }
        return 1;
    }

    @Override
    public void insertPurchase(PurchaseDto purchaseDto) {
        purchaseMapper.insert(purchaseDto);
    }

    @Override
    public void updatePurchase(PurchaseDto purchaseDto) {
        purchaseMapper.updateById(purchaseDto);
    }

    @Override
    public String deletePurchase(int id) {
        try {
            purchaseMapper.deleteById(id);
            return "删除成功";
        }catch (Exception e){
            return "删除失败";
        }
    }

    @Override
    public void deleteAll(int userId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_id",userId);
        purchaseMapper.delete(qw);
    }

}
